package com.cloudstore.app.a4af6d48384984d889eabd9fd40eb0d15.dao;

import java.util.ArrayList;
import java.util.List;

import weaver.conn.RecordSet;

import com.cloudstore.api.util.Util_Log;
import com.cloudstore.app.a4af6d48384984d889eabd9fd40eb0d15.dao.Dao_Salary;
import com.cloudstore.app.a4af6d48384984d889eabd9fd40eb0d15.entity.Salary;

public class Dao_SalaryImpl implements Dao_Salary {
	
	private String    sql = null;
	private RecordSet rs  = null;
	private Util_Log  l   = null;

	public Dao_SalaryImpl() {
		rs = new RecordSet();
		l = new Util_Log();
	}

	@Override
	public int findSalarySet(Salary s) {
		// TODO Auto-generated method stub
		StringBuilder sb = new StringBuilder();
		sb.append("select id from a4af6d48384984d889eabd9fd40eb0d15.salaryset where 1=1");
		sb.append(" and mainkey='").append(s.getMainKey()).append("'");
		sb.append(" and type='").append(s.getType()).append("'");
		sb.append(" and datestr='").append(s.getDate()).append("'");
		sb.append(" and hrId="+s.getHrId());
		sql = sb.toString();
		if(!rs.execute(sql)) l.write(sql);
		return rs.next()?rs.getInt(1):0;
	}

	@Override
	public int addSalarySet(Salary s) {
		// TODO Auto-generated method stub
		StringBuilder sb = new StringBuilder();
		if(rs.getDBType().equals("oracle")) {
			sb.append("insert into a4af6d48384984d889eabd9fd40eb0d15.salaryset(id,mainKey,type,datestr,fields,names,hrId,isSend,del)");
			sb.append(" values(a4af6d48384984d889eabd9fd40eb0d15.salaryset_seq.nextval,'").append(s.getMainKey()).append("','").append(s.getType()).append("','").append(s.getDate()).append("','");
			sb.append(s.getFields()).append("','").append(s.getNames()).append("',").append(s.getHrId()).append(",1,1)");
		}
		else {
			sb.append("insert into a4af6d48384984d889eabd9fd40eb0d15.salaryset(mainKey,type,datestr,fields,names,hrId,isSend,del)");
			sb.append(" values('").append(s.getMainKey()).append("','").append(s.getType()).append("','").append(s.getDate()).append("','");
			sb.append(s.getFields()).append("','").append(s.getNames()).append("',").append(s.getHrId()).append(",1,1)");
		}
		sql = sb.toString();
		boolean success = true;
		if(!rs.execute(sql)) {
			l.write(sql);
			success = false;
		}
		if(success) {
			sql = "select max(id) from a4af6d48384984d889eabd9fd40eb0d15.salaryset where del=1";
			if(!rs.execute(sql)) {
				success = false;
			}
			if(success) {
				return rs.next()?rs.getInt(1):0;
			}
		}
		return 0;
	}

	@Override
	public boolean editSalarySet(Salary s) {
		// TODO Auto-generated method stub
		StringBuilder sb = new StringBuilder();
		sb.append("update a4af6d48384984d889eabd9fd40eb0d15.salaryset set ");
		sb.append("fields='").append(s.getFields()).append("'");
		sb.append(",names='").append(s.getNames()).append("'");
		sb.append(",del=1");
		sb.append(" where mainKey='").append(s.getMainKey()).append("'");
		sb.append(" and type='").append(s.getType()).append("'");
		sb.append(" and datestr='").append(s.getDate()).append("'");
		sb.append(" and hrId=").append(s.getHrId());
		sql = sb.toString();
		if(!rs.execute(sql)) {
			l.write(sql);
			return false;
		}
		return true;
	}

	@Override
	public boolean findSalary(Salary s) {
		// TODO Auto-generated method stub
		StringBuilder sb = new StringBuilder();
		sb.append("select id from a4af6d48384984d889eabd9fd40eb0d15.salary where 1=1");
		sb.append(" and slid=").append(s.getId());
		sb.append(" and userId=").append(s.getUserId());
		String sql = sb.toString();
		if(!rs.execute(sql)) {
			l.write(sql);
			return true;
		}
		return rs.next();
	}

	@Override
	public boolean addSalary(Salary s) {
		// TODO Auto-generated method stub
		StringBuilder sb = new StringBuilder();
		if(rs.getDBType().equals("oracle")) {
			sb.append("insert into a4af6d48384984d889eabd9fd40eb0d15.salary(id,slid,userId,mainKey,valuedatas,del)");
			sb.append(" values(a4af6d48384984d889eabd9fd40eb0d15.salary_seq.nextval,").append(s.getId()).append(",").append(s.getUserId()).append(",'").append(s.getMainKey()).append("','").append(s.getValues()).append("',1)");
		}
		else {
			sb.append("insert into a4af6d48384984d889eabd9fd40eb0d15.salary(slid,userId,mainKey,valuedatas,del)");
			sb.append(" values(").append(s.getId()).append(",").append(s.getUserId()).append(",'").append(s.getMainKey()).append("','").append(s.getValues()).append("',1)");
		}
		sql = sb.toString();
		if(!rs.execute(sql)) {
			l.write(sql);
			return false;
		}
		return true;
	}

	@Override
	public boolean editSalary(Salary s) {
		// TODO Auto-generated method stub
		StringBuilder sb = new StringBuilder();
		sb.append("update a4af6d48384984d889eabd9fd40eb0d15.salary set ");
		sb.append(" mainKey='").append(s.getMainKey()).append("'");
		sb.append(",valuedatas='").append(s.getValues()).append("'");
		sb.append(",del=1");
		sb.append(" where slid=").append(s.getId());
		sb.append(" and userId=").append(s.getUserId());
		sql = sb.toString();
		if(!rs.execute(sql)) {
			l.write(sql);
			return false;
		}
		return true;
	}

	@Override
	public int findUserId(String mainKey, String value) {
		// TODO Auto-generated method stub
		StringBuilder sb = new StringBuilder();
		sb.append("select id from hrmresource where 1=1");
		if("idCard".equals(mainKey)) {
			sb.append(" and upper(certificatenum)=upper('").append(value).append("')");
		}
		else if("workCode".equals(mainKey)) {
			sb.append(" and workcode='").append(value).append("'");
		}
		else {
			sb.append(" and 1=2");
		}
		String sql = sb.toString();
		if(!rs.execute(sql)) l.write(sql);
		return rs.next()?rs.getInt(1):0;
	}

	@Override
	public boolean addFieldColumn(String name) {
		// TODO Auto-generated method stub
		sql = "select * from syscolumns where id=object_id('a4af6d48384984d889eabd9fd40eb0d15.salaryset') and name='"+name+"'";
		boolean find = false;
		if(!rs.execute(sql)) {
			l.write(sql);
			find = false;
		}
		find = rs.next();
		if(!find) {
			sql = "alter table a4af6d48384984d889eabd9fd40eb0d15.salary add column "+name+" varchar(512)";
			if(!rs.execute(sql))
				return false;
		}
		return true;
	}

	@Override
	public List<Salary> getSalaryListById(int userId,String date,boolean isHr) {
		// TODO Auto-generated method stub
		StringBuilder sb = new StringBuilder();
		sb.append("select b.mainKey,b.type,b.datestr,b.names,a.valuedatas,a.id,a.userId,b.id as setId,b.isSend,b.hrId from a4af6d48384984d889eabd9fd40eb0d15.salary a,a4af6d48384984d889eabd9fd40eb0d15.salaryset b ");
		sb.append(" where a.slid=b.id");
		if(!isHr) {
			sb.append(" and b.isSend=-1");
			sb.append(" and a.userId=").append(userId);
		}
		else sb.append(" and b.hrId=").append(userId);
		sb.append(" and b.datestr='").append(date).append("'");
		sb.append(" and a.del=1 and b.del=1");
		sb.append(" order by b.id,a.userId");
		sql = sb.toString();
		//l.write(sql);
		List<Salary> sl = new ArrayList<Salary>();
		if(!rs.execute(sql)) l.write(sql);
		while(rs.next()) {
			Salary s = new Salary();
			s.setMainKey(rs.getString(1));
			s.setType(rs.getString(2));
			s.setDate(rs.getString(3));
			s.setNames(rs.getString(4));
			s.setValues(rs.getString(5));
			s.setId(rs.getString(6));
			s.setMainKeyValue(rs.getString(7));
			s.setSetId(rs.getString(8));
			s.setIsSend(rs.getString(9));
			s.setHrId(rs.getString(10));
			sl.add(s);
		}
		return sl;
	}

	@Override
	public boolean sendSalaryBySetId(String setId) {
		// TODO Auto-generated method stub
		sql = "update a4af6d48384984d889eabd9fd40eb0d15.salaryset set isSend=-1*isSend where id="+setId;
		if(!rs.execute(sql)) {
			l.write(sql);
			return false;
		}
		return true;
	}

	@Override
	public boolean delSalaryBySetId(String setId) {
		// TODO Auto-generated method stub
		sql = "delete from a4af6d48384984d889eabd9fd40eb0d15.salary where slid="+setId;
		if(!rs.execute(sql)) {
			l.write(sql);
			return false;
		}
		sql = "delete from a4af6d48384984d889eabd9fd40eb0d15.salaryset where id="+setId;
		if(!rs.execute(sql)) {
			l.write(sql);
			return false;
		}
		return true;
	}

	@Override
	public List<String> getSalaryTypes(String mainKey, String date) {
		// TODO Auto-generated method stub
		sql = "select type from a4af6d48384984d889eabd9fd40eb0d15.salaryset where mainKey='"+mainKey+"' and dateStr='"+date+"' and del=1";
		List<String> sl = new ArrayList<String>();
		if(!rs.execute(sql)) {
			l.write(sql);
		}
		while(rs.next()) {
			sl.add(rs.getString(1));
		}
		return sl;
	}

	@Override
	public List<Salary> getSalaryDataByParams(String userIds,String mainKey,String date,String type) {
		// TODO Auto-generated method stub
		StringBuilder sb = new StringBuilder();
		sb.append("select x.id as userId,y.names,y.valuedatas,y.isSend,y.hrId,x.mobile,y.isMsgSend,y.id,y.mainKey,y.type,y.datestr,y.setId");
		sb.append(" from (select id,mobile from hrmresource where id in (").append(userIds).append(")) x left join ");
		sb.append("(select b.mainKey,b.type,b.datestr,b.names,a.valuedatas,a.id,a.userId,b.id as setId,b.isSend,b.hrId,b.isMsgSend ");
		sb.append(" from a4af6d48384984d889eabd9fd40eb0d15.salary a,a4af6d48384984d889eabd9fd40eb0d15.salaryset b");
		sb.append(" where a.del=1 and b.del=1 and a.slid=b.id) y on x.id=y.userId");
		sb.append(" and y.mainKey='").append(mainKey).append("'");
		sb.append(" and y.type='").append(type).append("'");
		sb.append(" and y.dateStr='").append(date).append("'");
		sql = sb.toString();
		if(!rs.execute(sql)) {
			l.write(sql);
		}
		List<Salary> sl = new ArrayList<Salary>();
		while(rs.next()) {
			Salary s = new Salary();
			s.setUserId(rs.getString(1));
			s.setNames(rs.getString(2));
			s.setValues(rs.getString(3));
			s.setIsSend(rs.getString(4));
			s.setHrId(rs.getString(5));
			s.setMobile(rs.getString(6));
			s.setIsMsgSend(rs.getString(7));
			s.setId(rs.getString(8));
			s.setKey(s.getUserId());
			sl.add(s);
		}
		return sl;
	}

	@Override
	public boolean markSalaryMsgSend(String id, String isMsgSend) {
		// TODO Auto-generated method stub
		sql = "update a4af6d48384984d889eabd9fd40eb0d15.salary set isMsgSend="+isMsgSend+" where id="+id;
		if(!rs.execute(sql)) {
			l.write(sql);
			return false;
		}
		return true;
	}

	@Override
	public String getRealMainKey(String mainKey, String mainKeyValue) {
		// TODO Auto-generated method stub
		String fieldName = "certificatenum";
		if("idCard".equals(mainKey)) {
			fieldName = "certificatenum ";
		}
		if("workCode".equals(mainKey)) {
			fieldName = "workcode";
		}
		sql = "select "+fieldName+" from hrmresource where id="+mainKeyValue;
		if(!rs.execute(sql)) {
			l.write(sql);
			return "";
		}
		return rs.next()?rs.getString(1):"";
	}

	
}